package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.EmployeeQuery;
import cn.wolfcode.rbac.service.IDepartmentService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.IRoleService;
import cn.wolfcode.rbac.util.JsonResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

//import com.alibaba.excel.EasyExcel;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value={"employee:list","员工列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo){
        // 查询全部部门
        model.addAttribute("depts",departmentService.listAll());

        model.addAttribute("result",employeeService.query(qo));
        return "employee/list";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value={"employee:saveOrUpdate","员工保存或修改"},logical= Logical.OR)
    public String saveOrUpdate(Employee employee,Long[] ids){
        if(employee.getId() == null){
            // 新增
            employeeService.save(employee,ids);
        }else{
            // 更改
            employeeService.update(employee,ids);
        }
        return "redirect:/employee/list.do";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value={"employee:input","员工编辑"},logical= Logical.OR)
    public String input(Model model,Long id){
        if(id != null){
            // 编辑
            Employee employee = employeeService.get(id);
            model.addAttribute("employee",employee);
        }
        // 查询全部部门
        model.addAttribute("depts",departmentService.listAll());

        // 查询全部角色
        model.addAttribute("roles",roleService.listAll());
        return "employee/input";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value={"employee:delete","员工删除"},logical= Logical.OR)
    public String delete(Long id){
        if (id != null) {
            employeeService.delete(id);
        }
        return "redirect:/employee/list.do";  // http://localhost/employee/list.do
    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    @RequiresPermissions(value={"employee:batchDelete","员工批量删除"},logical= Logical.OR)
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
       // return "redirect:/employee/list.do";  // http://localhost/employee/list.do
        return new JsonResult();
    }

    /**
     * 用户远程访问用户名是否存在
     * 返回 Map true 代表验证通过（用户名不存在），false 代表 验证不通过（用户名存在）
     * @param username
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String,Boolean> checkName(@RequestParam("name") String username){
        Employee employee = employeeService.selectByUsername(username);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid",employee==null);
        return map;
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        // 直接使用 POI
        // 创建 Excel 文件
        Workbook workbook = employeeService.exportXls();
        // 文件下载的响应头
        response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        // 把 Excel的数据输出给浏览器
        workbook.write(response.getOutputStream());

        // 使用 阿里的 easyExcel(写)
        // 文件下载的响应头
        //response.setHeader("Content-Disposition","attachment;filename=employee.xls");
        // 查询员工数据
        //List<Employee> employees = employeeService.listAll();
        //EasyExcel.write(response.getOutputStream(), Employee.class).sheet("员工名单").doWrite(employees);
    }

    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws IOException {
        System.out.println(file.getOriginalFilename());
        employeeService.importXls(file);
        return new JsonResult();
    }
}
